package etnorservice.etnor_service.controller;

import etnorservice.etnor_service.result.Result;
import etnorservice.etnor_service.service.ImageUploadService;
import etnorservice.etnor_service.utils.ImageCompressUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2025/03/19/12:13
 **/
@RestController
@Slf4j
public class EtnorImageController {

    @Autowired
    ImageUploadService imageUploadService;

    @Value("${server-image.base-url}")
    private String baseUrl;

    @PostMapping("/etnor/images/upload")
    public ResponseEntity<?> uploadImage(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "compress", defaultValue = "false") boolean compress) {
        try {
            String imageUrl = imageUploadService.uploadImage(file);

            if (compress) {
                // 获取源文件路径
                Path sourcePath = imageUploadService.getImagePath(imageUrl);
                // 生成压缩文件路径
                Path targetPath = Paths.get(sourcePath.toString() + "_compressed.jpg");
                // 压缩图片
                ImageCompressUtil.compressImage(sourcePath, targetPath, 0.7f);
                imageUrl = imageUrl + "_compressed.jpg";
            }

            Map<String, String> response = new HashMap<>();
            response.put("url", imageUrl);

            return ResponseEntity.ok(response);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (IOException e) {
            log.error("上传图片失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("上传图片失败");
        }
    }



    @PostMapping("/system/etnor/images/upload")
    public Result<?> uploadProjectImage(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "compress", defaultValue = "false") boolean compress) {
        try {
            String imageUrl = imageUploadService.uploadImage(file);

            if (compress) {
                // 获取源文件路径
                Path sourcePath = imageUploadService.getImagePath(imageUrl);
                // 生成压缩文件路径
                Path targetPath = Paths.get(sourcePath.toString() + "_compressed.jpg");
                // 压缩图片
                ImageCompressUtil.compressImage(sourcePath, targetPath, 0.7f);
                imageUrl = imageUrl + "_compressed.jpg";
            }

            Map<String, String> response = new HashMap<>();
            response.put("url", imageUrl);

            return Result.success(baseUrl+imageUrl);
        } catch (IllegalArgumentException e) {
            return Result.error("异常" +e.getMessage());
        } catch (IOException e) {
            log.error("上传图片失败", e);
            return Result.error("异常" +e.getMessage());
        }
    }


    @PostMapping("/test/images/upload")
    public Result<?> testUploadProjectImage(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "compress", defaultValue = "false") boolean compress) {
        try {
            String imageUrl = imageUploadService.uploadImage(file);

            if (compress) {
                // 获取源文件路径
                Path sourcePath = imageUploadService.getImagePath(imageUrl);
                // 生成压缩文件路径
                Path targetPath = Paths.get(sourcePath.toString() + "_compressed.jpg");
                // 压缩图片
                ImageCompressUtil.compressImage(sourcePath, targetPath, 0.7f);
                imageUrl = imageUrl + "_compressed.jpg";
            }

            Map<String, String> response = new HashMap<>();
            response.put("url", imageUrl);

            return Result.success(baseUrl+imageUrl);
        } catch (IllegalArgumentException e) {
            return Result.error("异常" +e.getMessage());
        } catch (IOException e) {
            log.error("上传图片失败", e);
            return Result.error("异常" +e.getMessage());
        }
    }


}